package com.banding.datastructure;

import java.util.function.Consumer;

/**
 * @param <E>
 * @author bd
 */
public class LinkedList<E> {

    private int size = 0;
    private Node<E> head;
    private Node<E> tail;

    /**
     * 返回链表元素个数
     */
    public int size() {
        return size;
    }

    /**
     * 判断链表是否为空
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 在末尾插入元素
     */
    public void add(E element) {
        add(size, element);
    }

    /**
     * 在指定位置插入新元素
     */
    public void add(int index, E element) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException(index);
        }
        if (isEmpty()) {
            Node<E> node = new Node<>(element, null);
            head = node;
            tail = node;
        } else if (index == 0) {
            head = new Node<>(element, head);
        } else if (index == size) {
            Node<E> node = new Node<>(element, null);
            tail.next = node;
            tail = node;
        } else {
            Node<E> previous = getNode(index - 1);
            Node<E> next = previous.next;
            previous.next = new Node<>(element, next);
        }
        size++;
    }

    private Node<E> getNode(int index) {
        Node<E> currentNode = head;
        for (int i = 0; i < index; i++) {
            currentNode = currentNode.next;
        }
        return currentNode;
    }

    /**
     * 获取指定位置的元素
     */
    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException(index);
        }
        Node<E> node = getNode(index);
        return node.item;
    }

    /**
     * 删除指定位置的元素，并返回删除内容
     */
    public E remove(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException(index);
        }
        E result = get(index);

        if (index == 0 && index == size - 1) {
            head = null;
            tail = null;
        } else if (index == 0) {
            head = head.next;
        } else if (index == size - 1) {
            Node<E> previous = getNode(index - 1);
            previous.next = null;
            tail = previous;
        } else {
            Node<E> previous = getNode(index - 1);
            previous.next = previous.next.next;
        }
        size--;
        return result;
    }

    /**
     * 修改指定位置的内容
     */
    public E set(int index, E element) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException(index);
        }
        Node<E> node = getNode(index);
        E item = node.item;
        node.item = element;
        return item;
    }

    /**
     * 遍历链表元素
     */
    public void forEach(Consumer<E> consumer) {
        Node<E> node = head;
        while (node != null) {
            consumer.accept(node.item);
            node = node.next;
        }
    }

    /**
     * 链表内容展示形式为：[1->4->2->3->5->null]
     *
     * @return 链表所有元素
     */
    @Override
    public String toString() {
        if (isEmpty()) {
            return "[]";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        forEach(o -> sb.append(o).append("->"));
        sb.append("null]");
        return sb.toString();
    }

    private static class Node<E> {
        E item;
        Node<E> next;

        Node(E item, Node<E> next) {
            this.item = item;
            this.next = next;
        }
    }


}
